package com.tiantian.quanminxiu.presenter;

import android.util.Log;

import com.shenglian.utils.utils.CommonUtils;
import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.FanRollView;

import org.json.JSONArray;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class FanRollPresent implements Presenter<FanRollView> {

    private FanRollView fanRollView;

    @Override
    public void attachView(FanRollView view) {
        this.fanRollView = view;
    }

    @Override
    public void detachView() {
        fanRollView = null;
    }


    public void getFanRollList(String User_ID,String OrderBy){
        fanRollView.Loading_Open();
        ApplicationBase app = ApplicationBase.get(fanRollView.getContext());
        AnchorService service = app.getAnchorService();
        service.contributionlist(UserData.Current.mid, UserData.Current.sign, Constants.APP_TYPE, 10, User_ID,OrderBy )
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .finallyDo(()->{
                    fanRollView.Loading_Close();
                })
                .subscribe(str -> {
                    Log.i("dd", "--data:" + str.toString());
                    fanRollView.getFanRollList(str.optJSONArray("data"));
                }, throwable -> {
                    CommonUtils.showToask(fanRollView.getContext(), throwable.getMessage());
                    fanRollView.Loading_Close();
                });

    }

    public void getGuardList(String User_ID){
        JSONArray guardArray = UserData.Current.GuardList;
        if(guardArray==null){
            throw new RuntimeException("守护列表出错，请重试");
        }else if(guardArray.length()>0){
            JSONObject object = guardArray.optJSONObject(0);
            if(!User_ID.equalsIgnoreCase(object.optString("User_ID_Guardian"))){
                throw new RuntimeException("守护列表信息出错，请重试");
            }
        }
        fanRollView.getGuradList(guardArray);
    }

}
